Completed
Push — development ( 5236fa...488938 )
by Stephen
20s
created

elk_DraftAutoSave.init   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/*!
2
 * @name      ElkArte Forum
3
 * @copyright ElkArte Forum contributors
4
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
5
 *
6
 * @version 1.1
7
 */
8
9
/**
10
 * This file contains javascript associated with the drafts auto function as it
11
 * relates to a plain text box (no sceditor invocation)
12
 */
13
14
/**
15
 * The constructor for the plain text box auto-saver
16
 *
17
 * @param {object} oOptions
18
 */
19
function elk_DraftAutoSave(oOptions)
20
{
21
	this.opt = oOptions;
22
	this.bInDraftMode = false;
23
	this.sCurDraftId = '';
24
	this.oCurDraftDiv = null;
25
	this.interval_id = null;
26
	this.oDraftHandle = document.forms.postmodify["message"];
0 ignored issues
show
Coding Style introduced by
['message'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
27
	this.sLastSaved = '';
28
	this.bCheckDraft = false;
29
30
	window.addEventListener("load", this.init.bind(this));
31
}
32
33
/**
34
 * Start our self calling routine
35
 */
36
elk_DraftAutoSave.prototype.init = function()
37
{
38
	if (this.opt.iFreq > 0)
39
	{
40
		// Start the autosaver timer
41
		this.interval_id = setInterval(this.draftSave.bind(this), this.opt.iFreq);
42
43
		// Set up the text area events
44
		this.oDraftHandle.onblur =  this.draftBlur.bind(this);
45
		this.oDraftHandle.onfocus = this.draftFocus.bind(this);
46
		this.oDraftHandle.onkeydown = function(oEvent) {
47
			// Don't let tabbing to the buttons trigger autosave event
48
			if (oEvent.keyCode === 9)
49
				this.bInDraftMode = true;
50
51
			return this.draftKeypress().bind(this);
52
		}.bind(this);
53
54
		// Prevent autosave when selecting post/save by mouse or keyboard
55
		var $_button = $('#postmodify').find('.button_submit');
56
		$_button .on('mousedown', this, function() {
57
			this.bInDraftMode = true;
58
		}.bind(this));
59
		$_button .on('onkeypress', this, function() {
60
			this.bInDraftMode = true;
61
		}.bind(this));
62
	}
63
};
64
65
/**
66
 * Moved away from the page, where did you go? ... till you return we pause autosaving
67
 *  - Handles the Blur event
68
 */
69
elk_DraftAutoSave.prototype.draftBlur = function()
70
{
71
	// Save if we are not already
72
	if (this.bInDraftMode !== true)
73
		this.draftSave();
74
	else
75
		this.draftCancel();
76
77
	if (this.interval_id !== "")
78
		window.clearInterval(this.interval_id);
79
80
	this.interval_id = "";
81
};
82
83
/**
84
 * Since your back we resume the autosave timer
85
 *  - Handles the focus event
86
 */
87
elk_DraftAutoSave.prototype.draftFocus = function()
88
{
89
	if (this.interval_id === "")
90
		this.interval_id = setInterval(this.draftSave.bind(this), this.opt.iFreq);
91
};
92
93
/**
94
 * Since your back we resume the autosave timer
95
 *  - Handles the keypress event
96
 */
97
elk_DraftAutoSave.prototype.draftKeypress = function()
98
{
99
	this.bCheckDraft = true;
100
};
101
102
/**
103
 * Makes the ajax call to save this draft in the background
104
 */
105
elk_DraftAutoSave.prototype.draftSave = function()
106
{
107
	// Form submitted or nothing changed since the last save
108
	if (elk_formSubmitted || !this.bCheckDraft)
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable elk_formSubmitted is declared in the current environment, consider using typeof elk_formSubmitted === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
109
		return false;
110
111
	// Still saving the last one or other?
112
	if (this.bInDraftMode)
113
		this.draftCancel();
114
115
	// Nothing to save?
116
	var sPostdata = document.forms.postmodify["message"].value;
0 ignored issues
show
Coding Style introduced by
['message'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
117
	if (isEmptyText(sPostdata) || !('topic' in document.forms.postmodify.elements))
118
		return false;
119
120
	// Flag that we are saving a draft
121
	document.getElementById('throbber').style.display = 'inline';
122
	this.bInDraftMode = true;
123
124
	// Get the form elements that we want to save
125
	var aSections = [
126
		'topic=' + parseInt(document.forms.postmodify.elements['topic'].value),
0 ignored issues
show
Coding Style introduced by
['topic'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
127
		'id_draft=' + (('id_draft' in document.forms.postmodify.elements) ? parseInt(document.forms.postmodify.elements['id_draft'].value) : 0),
0 ignored issues
show
Coding Style introduced by
['id_draft'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
128
		'subject=' + document.forms.postmodify['subject'].value.replace(/&#/g, "&#").php_urlencode(),
0 ignored issues
show
Coding Style introduced by
['subject'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
129
		'message=' + sPostdata.replace(/&#/g, "&#").php_urlencode(),
130
		'icon=' + document.forms.postmodify['icon'].value.replace(/&#/g, "&#").php_urlencode(),
0 ignored issues
show
Coding Style introduced by
['icon'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
131
		'save_draft=true',
132
		'autosave=true',
133
		elk_session_var + '=' + elk_session_id
0 ignored issues
show
Bug introduced by
The variable elk_session_var seems to be never declared. If this is a global, consider adding a /** global: elk_session_var */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable elk_session_id seems to be never declared. If this is a global, consider adding a /** global: elk_session_id */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
134
	];
135
136
	// Send in document for saving and hope for the best
137
	sendXMLDocument.call(this, elk_prepareScriptUrl(elk_scripturl) + "action=post2;board=" + this.opt.iBoard + ";xml", aSections.join("&"), this.onDraftDone);
0 ignored issues
show
Bug introduced by
The variable sendXMLDocument seems to be never declared. If this is a global, consider adding a /** global: sendXMLDocument */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable elk_scripturl seems to be never declared. If this is a global, consider adding a /** global: elk_scripturl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
138
139
	// Save the latest for compare
140
	this.bCheckDraft = false;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
141
};
142
143
/**
144
 * Callback function of the XMLhttp request for saving the draft message
145
 * @param {object} XMLDoc
146
 */
147
elk_DraftAutoSave.prototype.onDraftDone = function(XMLDoc)
148
{
149
	// If it is not valid then clean up
150
	if (!XMLDoc || !XMLDoc.getElementsByTagName('draft')[0])
151
		return this.draftCancel();
152
153
	// Grab the returned draft id and saved time from the response
154
	this.sCurDraftId = XMLDoc.getElementsByTagName('draft')[0].getAttribute('id');
155
	this.sLastSaved = XMLDoc.getElementsByTagName('draft')[0].childNodes[0].nodeValue;
156
157
	// Update the form to show we finished, if the id is not set, then set it
158
	document.getElementById(this.opt.sLastID).value = this.sCurDraftId;
159
	this.oCurDraftDiv = document.getElementById(this.opt.sLastNote);
160
	this.oCurDraftDiv.innerHTML = this.sLastSaved;
161
162
	// thank you sir, may I have another
163
	this.bInDraftMode = false;
164
	document.getElementById('throbber').style.display = 'none';
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
165
};
166
167
// If another auto save came in with one still pending
168
elk_DraftAutoSave.prototype.draftCancel = function()
169
{
170
	this.bInDraftMode = false;
171
	document.getElementById('throbber').style.display = 'none';
172
};